using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //RolesRadioButtonList.SelectedIndex = -1;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        String username = GridView1.SelectedRow.Cells[0].Text;
        String[] user_roles = Roles.GetRolesForUser(username);

        if (user_roles.Length != 0)
        {
            if (user_roles[0].Equals(RolesRadioButtonList.Items[0].ToString()))
                RolesRadioButtonList.SelectedIndex = 0;
            else if (user_roles[0].Equals(RolesRadioButtonList.Items[1].ToString()))
                RolesRadioButtonList.SelectedIndex = 1;
            else if (user_roles[0].Equals(RolesRadioButtonList.Items[2].ToString()))
                RolesRadioButtonList.SelectedIndex = 2;
            else if (user_roles[0].Equals(RolesRadioButtonList.Items[3].ToString()))
                RolesRadioButtonList.SelectedIndex = 3;
        }
        else
        {
            RolesRadioButtonList.SelectedIndex = -1;
        }
    }

    protected void AktualizujButton_Click(object sender, EventArgs e)
    {
        String username = GridView1.SelectedRow.Cells[0].Text;
        String[] user_roles = Roles.GetRolesForUser(username);
        String[] roles = Roles.GetAllRoles();
        
        if (user_roles.Length != 0)
            Roles.RemoveUserFromRoles(username, user_roles);
        
        if (RolesRadioButtonList.SelectedIndex == 0)
            Roles.AddUserToRole(username, roles[0]);
        else if (RolesRadioButtonList.SelectedIndex == 1)
            Roles.AddUserToRole(username, roles[1]);
        else if (RolesRadioButtonList.SelectedIndex == 2)
            Roles.AddUserToRole(username, roles[2]);
        else if (RolesRadioButtonList.SelectedIndex == 3)
            Roles.AddUserToRole(username, roles[3]);

        RolesRadioButtonList.SelectedIndex = -1;
    }

}
